package zdh.avatar.gui;

import zdh.avatar.softphone.Softphone;
import java.awt.*;
import javax.sound.sampled.Clip;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import zdh.avatar.*;
import zdh.avatar.crm.CRM;

/**
 *
 * @author Gem Seeker
 */
public class MainFrame extends JFrame {

    // GUI PROPERTIES
    private static final int width = 1000;
    private static final int height = 650;

    // APPLICATION PROPERTIES
    private final AvatarConfig config;
    private final String appName;
    private final String appVersion;
    private final String elementsPath;

    // GUI MAIN COMPONENTS
    private Menus menuBar;
    private ButtonGroups buttonGroups;
    private ChildGroup childGroup;
    private Softphone softphone;
    private CRM crm;

    // UTILITIES
    private boolean loaded = false;
    private String currentBrowsedDir = Utils.HOME;

    // TODO: Add sip account as parameters for this constructor
    public MainFrame(AvatarConfig config) {
        this.config = config;
        appName = config.getAppName();
        appVersion = config.getAppVersion();
        elementsPath = config.getElementsPath();
        initLookAndFeel();
        initComponents();
    }

    //<editor-fold defaultstate="collapsed" desc="initLookAndFeel">
    private void initLookAndFeel() {
        try {
            System.out.println(appName + " " + appVersion);
            System.out.print("Loading System's Look and Feel...");

            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

            System.out.println("OK");
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
//            System.err.println("ERROR >>> " + e.getMessage());
            System.out.println("FAILED");
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="initComponents">
    private void initComponents() {
        System.out.print("Loading App Preferences...");

        setTitle(appName + " " + appVersion);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);

        System.out.println("Done");

        BorderLayout layout = new BorderLayout();
        layout.setHgap(2);
        layout.setVgap(2);
        setLayout(layout);

        // MENU BAR
        System.out.print("Loading menu bar...");

        menuBar = new Menus(this, config);
        getRootPane().setJMenuBar(menuBar);

        System.out.println("Done");

        // PANEL FOR BUTTON AND CHILD GROUPS
        System.out.print("Loading button group panel...");
        JPanel panel;
        panel = new JPanel(new GridLayout(2, 0));
//        panel.setBackground(Color.DARK_GRAY);
//        panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        // BUTTON GROUPS
        buttonGroups = new ButtonGroups(this, config);
        panel.add(buttonGroups);

        childGroup = new ChildGroup(this, buttonGroups);
        panel.add(childGroup);

        add(panel, BorderLayout.CENTER);

        System.out.println("Done");
        System.out.print("Loading softphone panel...");

        softphone = new Softphone(this);
        add(softphone, BorderLayout.EAST);

        System.out.print("Loading CRM panel...");
        crm = new CRM(this);
        add(crm, BorderLayout.SOUTH);
        System.out.println("Done");

        System.out.println("Done");
        System.out.println("Loading Successful");
        System.out.println("Showing GUI");

        loaded = true;
        loadButtons();
    }
    //</editor-fold>

    // PUBLIC METHODS
    public void showAndCreateGui() {
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void reload() {
        if (buttonGroups != null) {
            buttonGroups.reload();
        }
    }

    public void loadButtons() {
        if (loaded) {
            buttonGroups.load(childGroup);
        }
    }

//    public void setWelcomeDisplay(boolean display) {
//        if (buttonGroups != null) {
//            if (true) {
//                if (!buttonGroups.welcomePanelDisplayed) {
//                    buttonGroups.showWelcomePanel();
//                }
//            }else{
//                if(buttonGroups.welcomePanelDisplayed){
//                    buttonGroups.hideWelcomePanel();
//                }
//            }
//        }
//    }

    // GETTERS and SETTERS
    public int getPanelCount() {
        return buttonGroups.getPanelCount();
    }

    public int getButtonCount() {
        return buttonGroups.getButtonCount();
    }

    public Clip getCurrentClip() {
        return buttonGroups.getCurrentClip();
    }

    public String getCurrentBrowsedDir() {
        return currentBrowsedDir;
    }

    public void setCurrentBrowsedDir(String dir) {
        currentBrowsedDir = dir;
    }

    public void setCurrentClip(Clip clip) {
        buttonGroups.setCurrentClip(clip);
    }
    
    public Menus getMenubar(){
        return menuBar;
    }
    
    public ButtonGroups getButtonGroups(){
        return buttonGroups;
    }

    public ChildGroup getChildGroup(){
        return childGroup;
    }
    
    public Softphone getSoftphone(){
        return softphone;
    }
    
    public CRM getCRM(){
        return crm;
    }
}
